<?php
	$TEMP->set_file(array('handle' => DIR_IFRAME_VIEW . 'changePicture.html'));
	
	include DIR_LIBRARY		. "PhotoUpload.php";
	
	$PHOTO 	= new WS_Photo_Upload();
	
	if ( $_POST['submitHidden'] == 1 ) {
		$if_error		= 0;
		$if_display		= 0;
		$filePhoto		= $_FILES['filePhoto'];
		$photoName 		= $filePhoto['name'];
		$photoExtension = $PHOTO->getExtension($photoName);
		$photoType 		= $filePhoto['type'];
		$photoTmpName 	= $filePhoto['tmp_name'];
		$photoError 	= $filePhoto['error'];
		$photoSize 		= $filePhoto['size'];
		$saveFileName	= md5(mktime() . '-' . $logUserID) . '.' . $photoExtension;
		
		if ( !empty($photoName)) {
			$allowedExts 	= array("gif", "jpeg", "jpg", "png");
			
			if ((($photoType == "image/gif") || ($photoType == "image/jpeg") || ($photoType == "image/jpg") || ($photoType == "image/png")) && ($photoSize < 1000000) && in_array($photoExtension, $allowedExts)) {
				if ($photoError > 0) {
					$uploadError = "Return Code: " . $photoError;
				}
				else {
					$arrProperties  = array('width' 		=> 150,
											'height' 		=> 150,
											'type' 			=> $photoType,
											'temp_name' 	=> $photoTmpName,
											'file_name' 	=> $saveFileName);
					$PHOTO->resizeUpload($arrProperties, DIR_UPLOAD);
					if (file_exists(DIR_UPLOAD . $saveFileName)) {
						$myPicture = '<div style="height:6px;"></div>
									  	<img src="' . DIR_UPLOAD . $saveFileName . '" border="0" width="120" height="120" style="border:1px solid #7c0304"/>
									  <div class="required-field cursor-pointer" style="padding-left:35px;" onclick="deletePicture(\'' . DIR_UPLOAD . $saveFileName . '\')"><img src="' . DIR_IMAGES . 'delete.jpg" border="0" width="48" height="14" border="0"/></div>';
					}
				}
			}
			else {
					$uploadError = "Processing Picture Error.";
			}
		}
		else {
			$uploadError = REQ_FIELD;
		}
	}
	else {
		$submitHidden = 0;
		$fileName 	  = $PI->getStudentPicture($logUserID);
		if (file_exists(DIR_UPLOAD . $fileName)) {
			$myPicture = '<div style="height:6px;"></div>
							<img src="' . DIR_UPLOAD . $fileName . '" border="0" width="120" height="120" style="border:1px solid #7c0304"/>
						  <div class="required-field cursor-pointer" style="padding-left:35px;" onclick="deletePicture(\'' . DIR_UPLOAD . $fileName . '\')"><img src="' . DIR_IMAGES . 'delete.jpg" border="0" width="48" height="14" border="0"/></div>';
		}
	}
	
	if ( isset($_POST['subButton']) ) {
		$fileName = _POST('saveFileName');
		if (!empty($fileName)) {
			$PI->changeStudentPicture($fileName, $logUserID);
			js_script('parent.window.location.href = parent.window.location.href;');
		}
		else {
			$uploadError = REQ_FIELD;
		}
	}
	
	$TEMP->set_var(array('uploadError' 	=> $uploadError,
						 'myPicture'	=> $myPicture,
						 'saveFileName'	=> $saveFileName,
						 'saved_record'	=> $saved_record
						 ));
?>